package cn.wolfcode.wolf2w.business.controller.admin;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import javax.servlet.http.HttpServletResponse;

import io.swagger.annotations.*;
import lombok.extern.java.Log;
import org.springframework.beans.factory.annotation.Autowired;
import cn.wolfcode.wolf2w.common.core.domain.R;
import com.baomidou.mybatisplus.core.metadata.IPage;
import cn.wolfcode.wolf2w.business.query.QuestionQuery;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
//import cn.wolfcode.wolf2w.common.log.annotation.Log;
//import cn.wolfcode.wolf2w.common.log.enums.BusinessType;
import cn.wolfcode.wolf2w.common.security.annotation.RequiresPermissions;
import cn.wolfcode.wolf2w.business.api.domain.Question;
import cn.wolfcode.wolf2w.business.service.IQuestionService;
import cn.wolfcode.wolf2w.common.core.web.controller.BaseController;
import cn.wolfcode.wolf2w.common.core.utils.poi.ExcelUtil;


/**
 * 提问Controller
 * 
 * @author hyz
 * @date 2024-08-13
 */
@RestController
@RequestMapping("/admin/questions")
@Api(value = "question问答控制器管理员接口",description = "question管理员资源控制器")
public class QuestionAdminController extends BaseController
{
    @Autowired
    private IQuestionService questionService;

    /**
     * 查询提问列表
     */
    @ApiOperation(value = "提问列表",notes = "查询提问列表")

    @ApiResponses({
            @ApiResponse(code = 200,message = "成功"),
            @ApiResponse(code = 201,message = "创建成功"),
            @ApiResponse(code = 401,message = "没登录"),
            @ApiResponse(code = 403,message = "没权限"),
            @ApiResponse(code = 404,message = "接口找不到"),
            @ApiResponse(code = 500,message = "服务器内部异常，请联系管理员"),
            @ApiResponse(code = 501,message = "接口业务异常")
    })
    @RequiresPermissions("business:question:list")
    @GetMapping("/list")
    public R<IPage<Question>> list(QuestionQuery qo) {
        return R.ok(questionService.queryPage(qo));
    }


    /**
     * 导出提问列表
     */
    @ApiOperation(value = "提问列表",notes = "导出提问列表")

    @ApiResponses({
            @ApiResponse(code = 200,message = "成功"),
            @ApiResponse(code = 201,message = "创建成功"),
            @ApiResponse(code = 401,message = "没登录"),
            @ApiResponse(code = 403,message = "没权限"),
            @ApiResponse(code = 404,message = "接口找不到"),
            @ApiResponse(code = 500,message = "服务器内部异常，请联系管理员"),
            @ApiResponse(code = 501,message = "接口业务异常")
    })
    @RequiresPermissions("business:question:export")
//    @Log(title = "提问", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Question question)
    {
        List<Question> list = questionService.list();
        ExcelUtil<Question> util = new ExcelUtil<Question>(Question.class);
        util.exportExcel(response, list, "提问数据");
    }

    /**
     * 获取提问详细信息
     */
    @ApiOperation(value = "提问详细信息",notes = "获取提问详细信息")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "id",name = "id",dataType = "Long",required = true)
    })
    @ApiResponses({
            @ApiResponse(code = 200,message = "成功"),
            @ApiResponse(code = 201,message = "创建成功"),
            @ApiResponse(code = 401,message = "没登录"),
            @ApiResponse(code = 403,message = "没权限"),
            @ApiResponse(code = 404,message = "接口找不到"),
            @ApiResponse(code = 500,message = "服务器内部异常，请联系管理员"),
            @ApiResponse(code = 501,message = "接口业务异常")
    })
    @RequiresPermissions("business:question:query")
    @GetMapping(value = "/{id}")
    public R<Question> getInfo(@PathVariable("id") Long id)
    {
        return R.ok(questionService.getById(id));
    }

    /**
     * 新增提问
     */
    @ApiOperation(value = "新增提问",notes = "新增提问")

    @ApiResponses({
            @ApiResponse(code = 200,message = "成功"),
            @ApiResponse(code = 201,message = "创建成功"),
            @ApiResponse(code = 401,message = "没登录"),
            @ApiResponse(code = 403,message = "没权限"),
            @ApiResponse(code = 404,message = "接口找不到"),
            @ApiResponse(code = 500,message = "服务器内部异常，请联系管理员"),
            @ApiResponse(code = 501,message = "接口业务异常")
    })
    @RequiresPermissions("business:question:add")
//    @Log(title = "提问", businessType = BusinessType.INSERT)
    @PostMapping
    public R<?> add(@RequestBody Question question)
    {
        return toAjax(questionService.save(question));
    }

    /**
     * 修改提问
     */
    @ApiOperation(value = "修改提问",notes = "修改提问")

    @ApiResponses({
            @ApiResponse(code = 200,message = "成功"),
            @ApiResponse(code = 201,message = "创建成功"),
            @ApiResponse(code = 401,message = "没登录"),
            @ApiResponse(code = 403,message = "没权限"),
            @ApiResponse(code = 404,message = "接口找不到"),
            @ApiResponse(code = 500,message = "服务器内部异常，请联系管理员"),
            @ApiResponse(code = 501,message = "接口业务异常")
    })
    @RequiresPermissions("business:question:edit")
//    @Log(title = "提问", businessType = BusinessType.UPDATE)
    @PutMapping
    public R<?> edit(@RequestBody Question question)
    {
        return toAjax(questionService.updateById(question));
    }

    /**
     * 删除提问
     */
    @ApiOperation(value = "删除提问",notes = "删除提问")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "id",name = "id",dataType = "Long",required = true)
    })
    @ApiResponses({
            @ApiResponse(code = 200,message = "成功"),
            @ApiResponse(code = 201,message = "创建成功"),
            @ApiResponse(code = 401,message = "没登录"),
            @ApiResponse(code = 403,message = "没权限"),
            @ApiResponse(code = 404,message = "接口找不到"),
            @ApiResponse(code = 500,message = "服务器内部异常，请联系管理员"),
            @ApiResponse(code = 501,message = "接口业务异常")
    })
    @RequiresPermissions("business:question:remove")
//    @Log(title = "提问", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public R<?> remove(@PathVariable Long[] ids)
    {
        return toAjax(questionService.removeByIds(Arrays.stream(ids).collect(Collectors.toList())));
    }
}
